﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Yeex.Boss.Web.Helper
{
    /// <summary>
    /// 需要登录之后才能访问
    /// </summary>
    public class NeedLoginAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// 在过程请求授权时调用。 
        /// </summary>
        /// <param name="filterContext">对象包括控制器、HTTP 上下文、请求上下文、操作结果和路由数据。</param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return;
            }

            FormsAuthentication.SignOut();
            string returnUrl = string.Format("{0}?returnUrl={1}", FormsAuthentication.LoginUrl, filterContext.HttpContext.Request.RawUrl);
            HttpContext.Current.Response.Redirect(returnUrl);
        }
    }
}